<div class="formbody">
    <div class="formtitle">
        <span>单位列表</span>
    </div>
    <ul class="forminfo">
    	<li style="display:flex;">
    	  <div style="width:300px;margin-left:100px;text-align:center;font-size:18px">学校名称</div><div style="width:300px;font-size:18px;text-align:center;">首充</div>
    	</li>
        <li>
            <label>&nbsp;</label>
            <input type="text" name="schoolName" value="" class="dfinput" id="addName">
            <input type="text"  name="firstChargePrice" value="50" class="dfinput" id="addPrice" ><i>(单位：元)</i>
            <button type="button" class="btn-inline" id="add">添加</button>
        </li>
    </ul>
    <ul class="forminfo" id="schoolList">
        <?php foreach ($schoolList as $v): ?>
           <li>
               <label><?php echo $v['id']; ?></label>
               <input type="text"  name="schoolName" value="<?php echo $v['name'] ?>" class="dfinput" >
               <input type="text"  name="firstChargePrice" value="<?php echo $v['firstChargePrice'] ?>" class="dfinput" >
               <button type="button" class="btn-inline hide">修改</button>
           </li>
        <?php endforeach; ?>

    </ul>
</div>
<script type="text/javascript">
    var urlPrefix = location.pathname.split('/',3).join('/')+'/';

    $('#schoolList li')
    .each(function(){
        var $li        = $(this);
        var $input     = $li.find('input[type="text"]');
        var $updateBtn = $li.find('button');
        var id   = parseInt($li.find('label').text());//获取学校ID
        var name = $input.val();// 初始的学校名

		var $inputPrice = $li.find('input[name="firstChargePrice"]');
		var price = $inputPrice.val();

        //学校输入框按键事件
        $input.on('keyup', function() {
            if( this.value != name ){
                $updateBtn.show();
            }else{
                $updateBtn.hide();
            }
        });

        //首充输入框按键事件
        $inputPrice.on('keyup', function() {
            if( this.value != price ){
                $updateBtn.show();
            }else{
                $updateBtn.hide();
            }
        });
       
        // 修改按钮事件
        $updateBtn.on('click',function(){
            var url  = urlPrefix+'update';
            var data = {
                id:id,
                name:$input.val(),
                price:$inputPrice.val()
            }
            $.post(url, data, function(rtn) {
                rtn = JSON.parse(rtn);
                if (rtn.error!==undefined) {
                    if( rtn.error!=0 ){
                        alert(rtn.msg);
                    }else{
                        alert('修改成功');
                        $updateBtn.hide();
                        location.reload();
                    }
                } else {
                    alert(rtn);
                }
            });
        })
    })
    // 添加按钮
    $('#add').click(function(){
        //var $input = $(this).prev('input.dfinput')
        var $input = $('#addName');
        var $inputPrice = $('#addPrice');

		if( ($input.val() == '') || ($inputPrice.val() == '') ){
			alert('名称或首充金额不得为空');
		}
        
        var data = {
            name : $input.val(),
            price :  $inputPrice.val()
        }
       
        var url  = urlPrefix+'add';
        $.post(url, data, function(rtn) {
            rtn = JSON.parse(rtn);
            if (rtn.error!==undefined) {
                if( rtn.error!=0 ){
                    alert(rtn.msg);
                }else{
                    alert('添加成功');
                    location.reload();
                    //window.location.reload()
                }
            } else {
                alert(rtn);
            }
        });
    })
</script>

